package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appmarket.service.deamon.bean.DownloadHistory;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import o.ewm.n;

public class dwk {
    private static int ˊ;
    private cki ˋ;
    private b ˎ = new b();
    private o.ejj.b ॱ;

    static class a implements OnDismissListener {
        private dwl ॱ;

        public a(dwl o_dwl) {
            this.ॱ = o_dwl;
        }

        public void onDismiss(DialogInterface dialogInterface) {
            if (this.ॱ != null && this.ॱ.ॱ() != null) {
                this.ॱ.ॱ().ॱ();
            }
        }
    }

    class b {
        final /* synthetic */ dwk ˋ;

        private b(dwk o_dwk) {
            this.ˋ = o_dwk;
        }

        private void ˊ(ewe o_ewe) {
            if (this.ˋ.ॱ != null) {
                this.ˋ.ॱ.ˊ();
            }
            if (o_ewe != null) {
                o_ewe.ॱ();
            }
        }

        private void ˏ(ewe o_ewe) {
            if (this.ˋ.ॱ != null) {
                this.ˋ.ॱ.ˏ();
            }
            if (o_ewe != null) {
                o_ewe.ॱ();
            }
        }

        private void ˏ(h hVar) {
            Context ˋ = hVar.ˋ();
            SessionDownloadTask ˊ = hVar.ˊ();
            ewe ॱ = hVar.ॱ();
            o.ckh.a ʽ = hVar.ʽ();
            dwl o_dwl = new dwl();
            o_dwl.ˏ(ˊ);
            o_dwl.ˏ(ॱ);
            o_dwl.ˏ(ʽ);
            if (dvr.ˏ(ˋ, true)) {
                SessionDownloadTask ˋ2 = dvt.ˏ().ˋ(ˊ.ˊˊ());
                if (ˊ.ˌ() == 9) {
                    ˋ2 = dvt.ˏ().ˎ(ˊ.ʾ());
                }
                if (ˋ2 == null) {
                    this.ˋ.ˏ(ˊ);
                    return;
                } else {
                    this.ˋ.ˊ(ˋ2);
                    return;
                }
            }
            long ˋ3 = dvr.ˋ(o_dwl);
            o.ckh.a eVar = new e(this.ˋ, o_dwl);
            OnDismissListener aVar = new a(o_dwl);
            o.ckh.e cVar = new c(o_dwl);
            if (dvr.ˏ(ˋ) || (doo.ʻ(ˋ) && ˊ.ˈ() == -1)) {
                dwk.ˊ = 1;
                dvr.ˏ(ˋ, ˋ3, eVar, aVar, cVar, null);
            } else if (dvr.ॱ(ˋ)) {
                this.ˋ.ˊ(ˊ, false);
            } else if (dvr.ˋ(ˋ) || (doo.ᐝ(ˋ) && ˊ.ˈ() == -1)) {
                dwk.ˊ = 2;
                dvr.ˊ(ˋ, ˋ3, eVar, aVar, cVar, null);
            }
        }

        private boolean ˎ(Context context, String str, SessionDownloadTask sessionDownloadTask) {
            if (!doo.ˊ(ContentManager.getInstant().getContext())) {
                if (context != null) {
                    evk.ˋ(context, context.getString(n.ʹˊ), 0).ˋ();
                }
                return true;
            } else if (sessionDownloadTask != null && !dof.ʽ(sessionDownloadTask.ˊˊ()) && context != null) {
                return false;
            } else {
                String str2;
                if (context != null) {
                    evk.ˏ(context, n.ʾˋ, 0).ˋ();
                }
                String str3 = "DownloadAdapter";
                StringBuilder append = new StringBuilder().append(str).append(" failed, downloadTask = ").append(sessionDownloadTask).append(", downloadTask.getPackageName_() = ");
                if (sessionDownloadTask == null) {
                    str2 = null;
                } else {
                    str2 = sessionDownloadTask.ˊˊ();
                }
                Logger.e(str3, append.append(str2).append(", context = ").append(context).toString());
                return true;
            }
        }
    }

    static class c implements o.ckh.e {
        private BroadcastReceiver ˊ;
        private dwl ˎ;

        public c(dwl o_dwl) {
            this.ˎ = o_dwl;
        }

        public void ॱ(Activity activity) {
            this.ˊ = new d(activity, this.ˎ);
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            activity.registerReceiver(this.ˊ, intentFilter);
        }

        public void ˋ(Activity activity) {
            if (this.ˊ != null) {
                activity.unregisterReceiver(this.ˊ);
            }
        }
    }

    static class d extends SafeBroadcastReceiver {
        private final WeakReference<Activity> ˊ;
        private dwl ˏ;

        public d(Activity activity, dwl o_dwl) {
            this.ˊ = new WeakReference(activity);
            this.ˏ = o_dwl;
        }

        public void onReceiveMsg(Context context, Intent intent) {
            if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
                NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra("networkInfo");
                if (networkInfo != null && State.CONNECTED == networkInfo.getState()) {
                    int ॱ = dvt.ॱ(context);
                    if (ॱ == 0 || dwk.ˊ == ॱ) {
                        Logger.ˎ("DownloadAdapter", "Keep current download dialog");
                        return;
                    }
                    Logger.ˎ("DownloadAdapter", "network has changed,close the download dialog, new net type=" + ॱ);
                    if (!(this.ˏ == null || this.ˏ.ॱ() == null)) {
                        this.ˏ.ॱ().ॱ();
                    }
                    Activity activity = (Activity) this.ˊ.get();
                    if (activity != null) {
                        activity.finish();
                    }
                }
            }
        }
    }

    class e implements o.ckh.a {
        private o.ckh.a ˋ;
        final /* synthetic */ dwk ˎ;
        private SessionDownloadTask ˏ;

        public e(dwk o_dwk, dwl o_dwl) {
            this.ˎ = o_dwk;
            this.ˏ = o_dwl.ˊ();
            this.ˋ = o_dwl.ʽ();
        }

        public void ˏ(AlertDialog alertDialog, com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a aVar, int i) {
            if (aVar.ˏ() == null) {
                Logger.e("DownloadAdapter", "view is null");
                return;
            }
            if (-1 == i) {
                dvr.ॱ(aVar.ˏ(), true);
                this.ˎ.ॱ(this.ˏ, true, false);
            } else if (-2 == i) {
                dvr.ॱ(aVar.ˏ(), false);
                this.ˎ.ॱ(this.ˏ, false, false);
            }
            if (this.ˋ != null) {
                this.ˋ.ˏ(alertDialog, aVar, i);
            }
        }
    }

    public static class h extends dwi {
        private Context ˏ;

        public Context ˋ() {
            return this.ˏ;
        }

        public void ˊ(Context context) {
            this.ˏ = context;
        }
    }

    public boolean ˏ(boolean z, final h hVar) {
        Context ˋ = hVar.ˋ();
        if (this.ˎ.ˎ(ˋ, "downloadTask", hVar.ˊ())) {
            return false;
        }
        final ewe ॱ = hVar.ॱ();
        if (!z) {
            this.ˎ.ˏ(hVar);
            if (this.ॱ == null) {
                return true;
            }
            this.ॱ.ॱ();
            return true;
        } else if (ejj.ˊ().ˎ()) {
            ejj.ˊ().ˏ(ˋ, new ejf(this) {
                final /* synthetic */ dwk ˋ;

                public void ˋ() {
                    this.ˋ.ˎ.ˏ(hVar);
                    this.ˋ.ˎ.ˊ(ॱ);
                }

                public void ॱ() {
                    this.ˋ.ˎ.ˏ(ॱ);
                }
            });
            return true;
        } else {
            this.ˎ.ˏ(hVar);
            if (this.ॱ == null) {
                return true;
            }
            this.ॱ.ॱ();
            return true;
        }
    }

    public boolean ॱ(h hVar) {
        SessionDownloadTask ˊ = hVar.ˊ();
        if (this.ˎ.ˎ(hVar.ˋ(), "resumeTask", ˊ)) {
            return false;
        }
        if (dvt.ˏ().ˎ(ˊ.ʾ()) != null) {
            this.ˎ.ˏ(hVar);
        }
        return true;
    }

    public void ॱ(cki o_cki) {
        this.ˋ = o_cki;
    }

    public void ॱ(final String str) {
        SessionDownloadTask ˋ = dvt.ˏ().ˋ(str);
        if (ˋ != null) {
            dvt.ˏ().ॱ(ˋ.ʾ());
        }
        new Thread(this, "cancelTask") {
            final /* synthetic */ dwk ˊ;

            public void run() {
                DownloadHistory ˊ = this.ˊ.ˎ(str);
                if (ˊ != null) {
                    ˊ.ˏ(3);
                    dsv.ˏ(ˊ);
                }
            }
        }.start();
    }

    public void ˏ(long j) {
        dvt.ˏ().ॱ(j);
        new f(j).start();
    }

    private void ˊ(SessionDownloadTask sessionDownloadTask) {
        ˎ(sessionDownloadTask, true);
    }

    public void ˎ(SessionDownloadTask sessionDownloadTask, boolean z) {
        if (sessionDownloadTask == null || dof.ʽ(sessionDownloadTask.ˊˊ()) || !doo.ˊ(ContentManager.getInstant().getContext())) {
            String str;
            String str2 = "DownloadAdapter";
            StringBuilder append = new StringBuilder().append("resumeDownload failed, downloadTask = ").append(sessionDownloadTask).append(", DeviceUtil.isConnectNet() = ").append(doo.ˊ(ContentManager.getInstant().getContext())).append(", downloadTask.getPackageName_() = ");
            if (sessionDownloadTask == null) {
                str = null;
            } else {
                str = sessionDownloadTask.ˊˊ();
            }
            Logger.e(str2, append.append(str).toString());
            return;
        }
        dvt.ˏ().ˎ(sessionDownloadTask);
        if (this.ˋ != null) {
            this.ˋ.ˋ();
        }
    }

    public int ˋ() {
        int i = 0;
        for (SessionDownloadTask sessionDownloadTask : dvt.ˏ().ͺ()) {
            int i2;
            if (dvt.ˏ().ˎ(sessionDownloadTask, false) && sessionDownloadTask.ʻ() && sessionDownloadTask.ˌ() != 9) {
                dvt.ˏ().ˏ(sessionDownloadTask.ʾ());
                i2 = i + 1;
            } else {
                i2 = i;
            }
            i = i2;
        }
        return i;
    }

    private DownloadHistory ˎ(String str) {
        if (dof.ʽ(str)) {
            return null;
        }
        return dsv.ॱ(str);
    }

    public SessionDownloadTask ˏ(String str) {
        return dvt.ˏ().ˋ(str);
    }

    public List<SessionDownloadTask> ॱ() {
        return dvt.ˏ().ͺ();
    }

    public List<SessionDownloadTask> ˎ() {
        List<SessionDownloadTask> arrayList = new ArrayList();
        arrayList.addAll(ॱ());
        return arrayList;
    }

    public SessionDownloadTask ˊ(String str) {
        if (dof.ʽ(str)) {
            return null;
        }
        for (SessionDownloadTask sessionDownloadTask : ˎ()) {
            if (str.equals(sessionDownloadTask.ˊˊ())) {
                break;
            }
        }
        SessionDownloadTask sessionDownloadTask2 = null;
        return sessionDownloadTask2;
    }

    public void ˊ(SessionDownloadTask sessionDownloadTask, boolean z) {
        if (!z) {
            dwb.ˋ(sessionDownloadTask.ʿ());
        }
        ॱ(sessionDownloadTask);
    }

    private void ॱ(SessionDownloadTask sessionDownloadTask) {
        dvt.ˏ().ॱ(sessionDownloadTask);
        List<SessionDownloadTask> ᐝˋ = sessionDownloadTask.ᐝˋ();
        if (ᐝˋ != null) {
            for (SessionDownloadTask ॱ : ᐝˋ) {
                dvt.ˏ().ॱ(ॱ);
            }
        }
        if (this.ˋ != null) {
            this.ˋ.ˋ();
        }
    }

    protected void ˏ(SessionDownloadTask sessionDownloadTask) {
        dvt.ˏ().ˏ(sessionDownloadTask);
        List<SessionDownloadTask> ᐝˋ = sessionDownloadTask.ᐝˋ();
        if (ᐝˋ != null) {
            for (SessionDownloadTask sessionDownloadTask2 : ᐝˋ) {
                SessionDownloadTask ˋ = dvt.ˏ().ˋ(sessionDownloadTask2.ˊˊ());
                if (ˋ == null) {
                    dvt.ˏ().ˏ(sessionDownloadTask2);
                } else {
                    dvt.ˏ().ˎ(ˋ);
                }
            }
        }
        if (this.ˋ != null) {
            this.ˋ.ˋ();
        }
    }

    public void ॱ(SessionDownloadTask sessionDownloadTask, boolean z, boolean z2) {
        if (z) {
            SessionDownloadTask ˋ = dvt.ˏ().ˋ(sessionDownloadTask.ˊˊ());
            if (sessionDownloadTask.ˌ() == 9) {
                ˋ = dvt.ˏ().ˎ(sessionDownloadTask.ʾ());
            }
            if (ˋ != null) {
                ˊ(ˋ);
                return;
            } else {
                ˏ(sessionDownloadTask);
                return;
            }
        }
        ˊ(sessionDownloadTask, z2);
    }

    public void ˊ(o.ejj.b bVar) {
        this.ॱ = bVar;
    }
}
